improve literal handling in pyi generator#5725
Conversation
There was a problem hiding this comment.
Greptile Summary
This PR enhances the pyi generator's type handling capabilities by addressing two specific gaps in literal and None value processing. The changes are made to the reflex/utils/pyi_generator.py file, which is responsible for generating .pyi stub files that provide type information for Python static analysis tools.
The first improvement expands None value detection in both the _get_type_hint() function (lines 148-149) and type_to_ast() function (lines 428-429). Previously, the code only checked for type(None) but missed direct None values, which could occur in type annotations. The new condition if value is type(None) or value is None: ensures both cases are properly handled and converted to the appropriate AST representation.
The second enhancement adds comprehensive support for typing.Literal types in the type_to_ast() function (lines 432-439). When the generator encounters a Literal type annotation (like Literal['red', 'green', 'blue']), it now creates a proper AST subscript node with the literal values as constants. This generates the correct stub representation: ast.Subscript with ast.Name(id="Literal") as the value and ast.Tuple containing ast.Constant nodes for each literal value.
These changes integrate seamlessly with the existing pyi generation pipeline. The PyiGenerator class uses these utility functions during its file scanning process to convert Python type annotations into their AST equivalents, which are then written to .pyi stub files. This ensures that modern Python typing constructs like Literal types are accurately preserved in the generated type stubs, maintaining compatibility with static analysis tools and IDEs.
Confidence score: 5/5
- This PR is safe to merge with minimal risk as it only adds missing type handling without changing existing logic
- Score reflects straightforward improvements to type conversion functions with clear, targeted fixes
- No files require special attention as the changes are isolated improvements to utility functions
1 file reviewed, no comments
CodSpeed Performance ReportMerging #5725 will not alter performanceComparing Summary
|
masenf
left a comment
There was a problem hiding this comment.
interesting that this doesn't change any current .pyi files... is this for a third party integration?
|
it's for reflex enterprise, it occasionally fails :/ |
No description provided.